package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApAriticleContentMapper;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.article.service.ArticleFreemakerService;
import com.heima.model.article.dto.ArticleDto;
import com.heima.model.article.dto.ArticleHomeDto;
import com.heima.model.article.pojo.ApArticle;
import com.heima.model.article.pojo.ApArticleConfig;
import com.heima.model.article.pojo.ApArticleContent;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: test
 * @Create by: JJ菜菜
 * @Date: 2022/1/2 21:01
 */
@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {

    @Autowired
    private ApArticleMapper apArticleMapper;

    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @Autowired
    private ApAriticleContentMapper apAriticleContentMapper;

    /**
     * 加载所有
     * @param dto
     * @return
     */
    @Override
    public ResponseResult load(ArticleHomeDto dto) {

        List<ApArticle> list =  apArticleMapper.load(dto);
        return ResponseResult.okResult(list);
    }

    /**
     * 下滑加载最新
     * @param dto
     * @return
     */
    @Override
    public ResponseResult loadnew(ArticleHomeDto dto) {
        List<ApArticle> list =  apArticleMapper.loadnew(dto);
        return ResponseResult.okResult(list);
    }

    /**
     * 上滑加载更多
     * @param dto
     * @return
     */
    @Override
    public ResponseResult loadmore(ArticleHomeDto dto) {
        List<ApArticle> list =  apArticleMapper.loadmore(dto);
        return ResponseResult.okResult(list);
    }

    @Autowired
    private ArticleFreemakerService articleFreemakerService;


    /**
     * 保存app端相关文章
     * @param dto
     * @return
     */
    @Override
    public ResponseResult saveArticle(ArticleDto dto) {

        // 检查参数
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        ApArticle apArticle = new ApArticle();
        BeanUtils.copyProperties(dto, apArticle);

        // 判断是否存在id
        if (dto.getId() == null){
            // 不存在保存 文章|文章配置|文章内容
            // 保存文章
            save(apArticle);

            // 保存文章配置, 当保存了article之后会生成主键，
            ApArticleConfig apArticleConfig = new ApArticleConfig(apArticle.getId());
            apArticleConfigMapper.insert(apArticleConfig);

            // 保存文章内容
            ApArticleContent apArticleContent = new ApArticleContent();
            apArticleContent.setArticleId(apArticle.getId());
            apArticleContent.setContent(dto.getContent());
            apAriticleContentMapper.insert(apArticleContent);

        }else {
            // 存在就修改 文章|文章配置|文章内容

            // 修改文章
            updateById(apArticle);

            // 先根据文章id查询出来，然后再设置文章内容，再去修改
            ApArticleContent apArticleContent = apAriticleContentMapper.selectOne(Wrappers.<ApArticleContent>lambdaQuery()
                    .eq(ApArticleContent::getArticleId, dto.getId()));

            apArticleContent.setContent(dto.getContent());
            // 修改文章内容
            apAriticleContentMapper.updateById(apArticleContent);
        }

        // 异步调用， 生成静态文件上传到minio中
        articleFreemakerService.buildArticleToMinio(apArticle, dto.getContent());

        return ResponseResult.okResult(apArticle.getId());
    }
}
